/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import view.Main;
import model.Customer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Mikkel & Jeppe
 */
public class CustomerHandler {

    private final static String TABEL = "Customer";
    private SQLCommunication sqlc;
    private ArrayList<Customer> customerList;

    public CustomerHandler(Main main) {
        this.sqlc = main.getSql();
        customerList = new ArrayList<>();
    }

    private void instantierCustomerList() {
        customerList.clear();
        int ID;
        int Phoneno;
        String companyName;
        String address;
        String zipcode;
        String city;
        String district;
        String country;

        String sql = "SELECT * FROM " + TABEL;
        ResultSet rs = sqlc.select(TABEL, sql);

        try {
            while (rs.next()) {
                ID = rs.getInt("ID");
                Phoneno = rs.getInt("Phoneno");
                companyName = rs.getString("companyName");
                address = rs.getString("Address");
                zipcode = rs.getString("Zipcode");
                city = rs.getString("City");
                district = rs.getString("District");
                country = rs.getString("Country");
                Customer customer = new Customer(ID, Phoneno, companyName, address, zipcode, city, district, country);
                customerList.add(customer);
            }
        } catch (SQLException ex) {
            System.out.println("Fejl ved instantiering af CustomerList");
        }
//        System.out.println(customerList.size());
    }

    public ArrayList<Customer> getCustomerList() {
        instantierCustomerList();
        return customerList;
    }

    public boolean tjekCustomerByPhoneNo(int phoneNo) {
        boolean exists = false;
        String sql = "SELECT * FROM " + TABEL + " WHERE phoneNo = " + phoneNo;
        ResultSet rs = sqlc.select(TABEL, sql);
        try {
            while (rs.next()) {
                exists = true;
            }
        } catch (SQLException ex) {
            System.out.println("Fejl ved hentning af customer");
        }
        return exists;
    }

    public Customer getCustomerByPhoneNo(int phoneNo) {
        Customer customer = null;
        int ID;
        String companyName;
        String address;
        String zipcode;
        String city;
        String district;
        String country;

        String sql = "SELECT * FROM " + TABEL + " WHERE phoneNo = " + phoneNo;
        ResultSet rs = sqlc.select(TABEL, sql);
        if (rs != null) {
            try {
                if (rs.next()) {
                    ID = rs.getInt("ID");
                    phoneNo = rs.getInt("Phoneno");
                    companyName = rs.getString("CompanyName");
                    address = rs.getString("Address");
                    zipcode = rs.getString("Zipcode");
                    city = rs.getString("City");
                    district = rs.getString("District");
                    country = rs.getString("Country");
                    customer = new Customer(ID, phoneNo, companyName, address, zipcode, city, district, country);
                }
            } catch (SQLException ex) {
                System.out.println("Fejl ved hentning af customer");
            }
        }
        return customer;
    }

    public void insertToDB(Customer customer) {
        String sql = "insert into " + TABEL + " (PhoneNo, CompanyName, Address, Zipcode, City, District, Country) values" + customer.toStringSQL();
        sqlc.insert(TABEL, sql);
    }
}
